﻿//=====================================================================
//
// File: SqlFilestream.SqlNativeClient
//
// Summary: Reusable class for providing access to DLL for reading and 
//   writing to varbinary(max) FILESTREAM.
//
// Date: November 11, 2007
//
// Authors:
//   Roger Doherty - http://blogs.msdn.com/rdoherty/
//   Zach Skyles Owens - http://blogs.msdn.com/zowens/
//
//---------------------------------------------------------------------
//
// This file is part of the Microsoft SQL Server Community Samples.
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation.  See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF 
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
//===================================================================== 
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

namespace SqlFilestream
{
    public class SqlNativeClient
    {
        public const UInt32 DESIRED_ACCESS_READ = 0x00000000;
        public const UInt32 DESIRED_ACCESS_WRITE = 0x00000001;
        public const UInt32 DESIRED_ACCESS_READWRITE = 0x00000002;

        [DllImport("sqlncli10.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern SafeFileHandle OpenSqlFilestream(
                    string FilestreamPath,
                    UInt32 DesiredAccess,
                    UInt32 OpenOptions,
                    byte[] FilestreamTransactionContext,
                    UInt32 FilestreamTransactionContextLength,
                    LARGE_INTEGER_SQL AllocationSize);

        [StructLayout(LayoutKind.Sequential)]
        public struct LARGE_INTEGER_SQL
        {
            public Int64 QuadPart;
            public LARGE_INTEGER_SQL(Int64 quadPart) { QuadPart = quadPart; }
        }

        //Get Last Error to check if the OpenSqlFilestream succeeded in returning a valid / handle
        //
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern UInt32 GetLastError();
    }
}
